package com.iflysse.controller;


import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.StudentAnswerQuestionBoReq;
import com.iflysse.pojo.bo.req.StudentIdAndTaskIdBoReq;
import com.iflysse.pojo.bo.req.StudentIdBoReq;
import com.iflysse.pojo.bo.req.StudentTestIdBoReq;
import com.iflysse.pojo.bo.resp.*;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.StudentAnswerQuestionVoReq;
import com.iflysse.pojo.vo.req.StudentIdAndTaskIdVoReq;
import com.iflysse.pojo.vo.req.StudentIdVoReq;
import com.iflysse.pojo.vo.req.StudentTestIdVoReq;
import com.iflysse.pojo.vo.resp.*;
import com.iflysse.services.TestService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/studentTest")
@Api(value = "学生端学生学习的接口")
@Log4j
public class StudentTestController {


    @Autowired
    private TestService testService;

    /**
     * 查询学生的某次测试的结果
     */
    @PostMapping("/queryStudentTestDetail")
    @ApiOperation("查询学生的某次测试的结果")
    public StudentTestDetailVoResp queryStudentTestDetail(StudentTestIdVoReq studentTestIdVoReq){
        StudentTestDetailVoResp studentTestDetailVoResp=new StudentTestDetailVoResp();
        try{

            log.info("查询学生的某次测试的结果参数bean:"+studentTestIdVoReq);
            StudentTestIdBoReq studentTestIdBoReq= CommonUtil.objToObj(studentTestIdVoReq,StudentTestIdBoReq.class);
            StudentTestDetailBoResp studentTestDetailBoResp= testService.getStudentTestDetailByStudentTestId(studentTestIdBoReq);
            StudentTestDetailResp studentTestDetailResp=   CommonUtil.objToObj(studentTestDetailBoResp,StudentTestDetailResp.class);
            studentTestDetailResp.setPublishDate(CommonUtil.dateSourceToString(studentTestDetailBoResp.getPublishDate(),CommonUtil.pattern));
            studentTestDetailVoResp.setResult(studentTestDetailResp);
            studentTestDetailVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            studentTestDetailVoResp.setCode(ResultEnum.SUCCESS.getCode());
            log.info("查询学生的某次测试的结果响应bean:"+studentTestDetailVoResp);
            return studentTestDetailVoResp;
        }catch (Exception e){
            log.error("学生端查询某次测试详情出现异常:"+e);
            CommonUtil.printError(studentTestDetailVoResp);
            return studentTestDetailVoResp;
        }

    }




    /**
     * 学生端   个人信息面板下 课后练习的数量  接口
     */
    @PostMapping("/queryTestCount")
    @ApiOperation("学生端个人信息面板下课后练习的数量接口")
    public StudentWaitTestCountVoResp queryTestCount(StudentIdVoReq studentIdVoReq){
        StudentWaitTestCountVoResp studentWaitTestCountVoResp=new StudentWaitTestCountVoResp();

        try{
            log.info("学生端个人信息面板下,课后练习的数量接口参数："+studentIdVoReq);
            StudentIdBoReq studentIdBoReq=CommonUtil.objToObj(studentIdVoReq, StudentIdBoReq.class);
            log.info("学生端个人信息面板下,课后练习的数量业务接口参数bean："+studentIdBoReq);
            UnfinishedTestAmountBoResp unfinishedTestAmountBoResp= testService.getUnfinishedTestAmountByStudentId(studentIdBoReq);
            log.info("学生端个人信息面板下,课后练习的数量业务接口响应bean:"+unfinishedTestAmountBoResp);
            StudentWaitTestCountResp studentWaitTestCountResp=CommonUtil.objToObj(unfinishedTestAmountBoResp,StudentWaitTestCountResp.class);
            studentWaitTestCountResp.setCount(unfinishedTestAmountBoResp.getAmount());
            studentWaitTestCountVoResp.setResult(studentWaitTestCountResp);
            studentWaitTestCountVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentWaitTestCountVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端个人信息面板下,课后练习的数量响应bean:"+studentWaitTestCountVoResp);
            return studentWaitTestCountVoResp;
        }catch (Exception e){
            log.error("学生端个人信息面板下课后练习的数量接口出现异常："+e);
            CommonUtil.printError(studentWaitTestCountVoResp);
            return studentWaitTestCountVoResp;
        }

    }


    /**
     * 学生交卷接口
     */
    @PostMapping("/submitTest")
    @ApiOperation("学生交卷接口")
    public CommonVoResp submitTest(StudentTestIdVoReq studentTestIdVoReq){
        CommonVoResp commonVoResp=new CommonVoResp();
        try{
            log.info("学生交卷接口参数bean:"+studentTestIdVoReq);
            StudentTestIdBoReq studentTestIdBoReq=CommonUtil.objToObj(studentTestIdVoReq,StudentTestIdBoReq.class);
            log.info("学生交卷业务接口参数bean:"+studentTestIdBoReq);
            CommonBoResp commonBoResp= testService.submitTest(studentTestIdBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("学生交卷接口响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("学生交卷出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    /**
     * 学生答题接口   回答每一题的接口
     */
    @PostMapping("/answerQuestion")
    @ApiOperation("学生答题接口 回答每一题的接口")
    public CommonVoResp answerQuestion(StudentAnswerQuestionVoReq studentAnswerQuestionVoReq) {
        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("学生答题接口,回答每一题的接口参数bean:"+studentAnswerQuestionVoReq);

            StudentAnswerQuestionBoReq studentAnswerQuestionBoReq=CommonUtil.objToObj(studentAnswerQuestionVoReq,StudentAnswerQuestionBoReq.class);
            log.info("学生答题接口,回答每一题的业务接口参数bean:"+studentAnswerQuestionBoReq);
            CommonBoResp commonBoResp= testService.answerQuestionByStudentIdAndStudentTestId(studentAnswerQuestionBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("学生答题接口,回答每一题的接口响应bean:"+commonVoResp);

            return commonVoResp;
        }catch (Exception e){
            log.error("学生答题接口,回答每一题的接口出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }




    /**
     * 学生点击开始做题  进入答题页面的信息（头部信息——>测试76，左侧列表）
     */
    @PostMapping("/queryAnswerTestInfo")
    @ApiOperation("学生点击开始做题  进入答题页面的信息（头部信息——>测试76，左侧列表）")
    public StudentAnswerTestInfoVoResp queryAnswerTestInfo(StudentIdAndTaskIdVoReq studentIdAndTaskIdVoReq){
        StudentAnswerTestInfoVoResp studentAnswerTestInfoVoResp=new StudentAnswerTestInfoVoResp();
        try{
            log.info("学生点击开始做题,进入答题页面的信息（头部信息——>测试76，左侧列表）业务参数："+studentIdAndTaskIdVoReq);
            StudentIdAndTaskIdBoReq studentIdAndTaskIdBoReq=CommonUtil.objToObj(studentIdAndTaskIdVoReq,StudentIdAndTaskIdBoReq.class);
            StudentAnswerBoResp studentAnswerBoResp= testService.startToAnswerByTestIdAndStudentId(studentIdAndTaskIdBoReq);
            StudentAnswerTestInfoResp result=CommonUtil.objToObj(studentAnswerBoResp,StudentAnswerTestInfoResp.class);
            studentAnswerTestInfoVoResp.setResult(result);
            studentAnswerTestInfoVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentAnswerTestInfoVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生点击开始做题,进入答题页面的信息（头部信息——>测试76，左侧列表）响应bean:"+studentAnswerTestInfoVoResp);

            return studentAnswerTestInfoVoResp;
        }catch (Exception e){
            log.error("学生点击开始做题,进入答题页面的信息（头部信息——>测试76，左侧列表）出现异常"+e);
            CommonUtil.printError(studentAnswerTestInfoVoResp);
            return studentAnswerTestInfoVoResp;
        }


    }


    /**
     * 当前学生测试答题页面下面的每次测试记录      下方的测试记录列表
     */
    @PostMapping("/queryStudentAnswerList")
    @ApiOperation("当前学生测试答题页面下面的每次测试记录,下方的测试记录列表")
    public StudentAnswerListVoResp queryStudentAnswerList(StudentIdAndTaskIdVoReq studentIdAndTaskIdVoReq){

        StudentAnswerListVoResp studentAnswerListVoResp=new StudentAnswerListVoResp();

        try{

            log.info("学生端查询本人测试记录列表参数bean:"+studentIdAndTaskIdVoReq);
            StudentIdAndTaskIdBoReq studentIdAndTaskIdBoReq=CommonUtil.objToObj(studentIdAndTaskIdVoReq,StudentIdAndTaskIdBoReq.class);
            StudentAnswerListBoResp studentAnswerListBoResp=testService.getStudentAnswerListByTestIdAndStudentId(studentIdAndTaskIdBoReq);
            log.info("学生端查询本人测试记录列表业务接口响应bean:"+studentAnswerListBoResp);
            List<StudentAnswerResp> result=new ArrayList<StudentAnswerResp>();
            studentAnswerListBoResp.getStudentAnswerRecordList().forEach(studentAnswerRecordBoResp -> {
                StudentAnswerResp studentAnswerResp= CommonUtil.objToObj(studentAnswerRecordBoResp, StudentAnswerResp.class);
                studentAnswerResp.setTestTime(CommonUtil.dateSourceToString(studentAnswerRecordBoResp.getTestTime(),CommonUtil.pattern));
                result.add(studentAnswerResp);
            });

            studentAnswerListVoResp.setResult(result);
            studentAnswerListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentAnswerListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端查询本人测试记录列表响应bean:"+studentAnswerListVoResp);
            return studentAnswerListVoResp;

        }catch (Exception e){
            log.error("当前学生测试答题页面下方的测试记录列表"+e);
            CommonUtil.printError(studentAnswerListVoResp);
            return studentAnswerListVoResp;
        }

    }



    /**
     * 学生点击某个测试进去的数据业务     上方信息
     * 根据学生学号和测试编号,查询测试详情
     */
    @PostMapping("/queryStudentTestInfo")
    @ApiOperation("学生点击某个测试进去的数据业务上方信息,根据学生学号和测试编号,查询测试详情")
    public StudentTestInfoVoResp queryStudentTestInfo(StudentIdAndTaskIdVoReq studentIdAndTaskIdVoReq){

        StudentTestInfoVoResp studentTestInfoVoResp=new StudentTestInfoVoResp();

        try{
            log.info("学生端查询测试头部详情信息参数bean:"+studentIdAndTaskIdVoReq);
            StudentIdAndTaskIdBoReq studentIdAndTaskIdBoReq=CommonUtil.objToObj(studentIdAndTaskIdVoReq,StudentIdAndTaskIdBoReq.class);
            log.info("学生端查询测试头部详情信息业务接口参数bean:"+studentIdAndTaskIdBoReq);
            StudentTestStateBoResp studentTestStateBoResp= testService.getStudentTestAndStateByTestIdAndStudentId(studentIdAndTaskIdBoReq);
            log.info("学生端查询测试头部详情信息业务响应bean:"+studentTestStateBoResp);
            StudentTestInfoResp result=CommonUtil.objToObj(studentTestStateBoResp,StudentTestInfoResp.class);
            result.setExpirationDate(CommonUtil.dateSourceToString(studentTestStateBoResp.getExpirationDate(),CommonUtil.pattern));
            result.setPublishDate(CommonUtil.dateSourceToString(studentTestStateBoResp.getPublishDate(),CommonUtil.pattern));
            studentTestInfoVoResp.setResult(result);
            studentTestInfoVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentTestInfoVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端查询测试头部详情信息响应bean:"+studentTestInfoVoResp);

            return studentTestInfoVoResp;
        }catch (Exception e){
            log.error("学生端查询测试头部详情信息出现异常："+e);
            CommonUtil.printError(studentTestInfoVoResp);
            return studentTestInfoVoResp;
        }


    }



    /**
     * 学生端查询本人课后练习的列表  从学生端首页点击课后练习的数量跳转到课后练习的列表
     */
    @PostMapping("/queryTestList")
    @ApiOperation("学生端查询本人课后练习的列表,从学生端首页点击课后练习的数量跳转到课后练习的列表")
    public StudentTeachingTestListVoResp queryTestList(StudentIdVoReq studentIdVoReq) {
        StudentTeachingTestListVoResp studentTeachingTestListVoResp=new StudentTeachingTestListVoResp();
        try{

            log.info("学生端查询本人课后练习的列表参数："+studentIdVoReq);
            StudentIdBoReq studentIdBoReq=CommonUtil.objToObj(studentIdVoReq,StudentIdBoReq.class);
            log.info("学生端查询本人课后练习的列表业务接口参数："+studentIdBoReq);
            TeachingActivityListBoResp teachingActivityListBoResp= testService.getTestByStudentId(studentIdBoReq);
            log.info("学生端查询本人课后练习的列表业务响应bean"+teachingActivityListBoResp);
            List<StudentTeachingTestListResp> result=new ArrayList<StudentTeachingTestListResp>();
            teachingActivityListBoResp.getTeachingActivityList().forEach(teachingActivityBoResp -> {
                StudentTeachingTestListResp studentTeachingTestListResp=CommonUtil.objToObj(teachingActivityBoResp,StudentTeachingTestListResp.class);
                studentTeachingTestListResp.setUpdateTime(CommonUtil.dateSourceToString(teachingActivityBoResp.getUpdateTime(),CommonUtil.pattern));
                studentTeachingTestListResp.setExpirationDate(CommonUtil.dateSourceToString(teachingActivityBoResp.getExpirationDate(),CommonUtil.pattern));
                studentTeachingTestListResp.setState(Integer.valueOf(teachingActivityBoResp.getState().getStateId()));
                result.add(studentTeachingTestListResp);
            });
            studentTeachingTestListVoResp.setResult(result);
            studentTeachingTestListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentTeachingTestListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("学生端查询本人课后练习的列表响应bean:"+studentTeachingTestListVoResp);
            return studentTeachingTestListVoResp;
        }catch (Exception e){
            log.error("学生端查询本人课后练习的列表出现异常："+e);
            CommonUtil.printError(studentTeachingTestListVoResp);
            return studentTeachingTestListVoResp;
        }

    }



}
